/*
 * @(#)SqlUrlIterator.java	0.1 01/01/04
 *
 * Copyright 2004 Fabio Gasparetti. All rights reserved.
 */
package org.tabularium.net.linkdb;

/**
 * Implementation of <code>UrlIterator</code> returned by
 * <code>SqlLinkDb</code>.
 * 
 * @author Fabio Gasparetti
 * @version 0.1, 01/01/06
 */

public class SqlUrlIterator implements UrlIterator {
	protected int[] linkIds;

	protected SqlLinkDb linkDb;

	protected int currentLink = 0;

	public SqlUrlIterator(SqlLinkDb linkDb, int[] links) {
		this.linkDb = linkDb;
		this.linkIds = links;
	}

	/**
	 * Returns true if the iteration has more links.
	 */
	public boolean hasNext() {
		return currentLink < linkIds.length;
	}

	/**
	 * Returns the next link in the iteration.
	 */
	public Object next() {
		String link = null;
		if (currentLink < linkIds.length) {
			try {
				link = linkDb.getUrlById(linkIds[currentLink]);
			} catch (Exception ex) {
				throw new RuntimeException(
						"LinkDb.getUrlById() raised exception: "
								+ ex.toString());
			}
			currentLink++;
		}
		return link;
	}

	/**
	 * Removes from the underlying collection the last link returned by the
	 * iterator.
	 * <p>
	 * Warninig: yet to be implemented.
	 */
	public void remove() {
		/** @todo to implement */
		/*
		 * if ((currentLink > 0) && (currentLink <= linkIds.length)) { try {
		 * linkDb.remove(linkIds[currentLink-1]); } catch (Exception ex) {
		 * logger.severe(ex.toString()); } }
		 */
	}
}
